<?php


namespace app\admin\controller;


use think\facade\Filesystem;

class Image extends AdminBase
{
    //图片上传
    public function upload()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '请求不合法');
        }
        $files = $this->request->file();

        $file = $this->request->file("file");
        //验证图片
        $validate = new \app\admin\validate\Image();
        if (!$validate->check($files)) {
            return show(config("status.error"), $validate->getError());
        }

        // $fileName = Filesystem::putFile('upload', $file);
        $fileName = Filesystem::disk('public')->putFile('image', $file);  //自定义 目录 地址

        if (!$fileName) {
            return show(config("status.error"), '上传图片失败');
        }
        $imageUrl = [
            "image" => "/upload/" . $fileName
        ];
        return show(config("status.success"), '图片上传成功', $imageUrl);
    }

    //文本编辑器 图片上传
    public function layUpload()
    {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '请求不合法');
        }

        $files = $this->request->file();

        $file = $this->request->file("file");
        //验证图片
        $validate = new \app\admin\validate\Image();
        if (!$validate->check($files)) {

            return json(["code" => 1, "data" => [], "msg" => $validate->getError()], 200);
        }

        $fileName = Filesystem::disk('public')->putFile('image', $file);  //图片移动到 自定义 目录 地址
        if (!$fileName) {
            return json(["code" => 1, "data" => []], 200);
        }
        $result = [
            "code" => 0,
            "data" => [
                "src" => "/upload/" . $fileName,
            ]
        ];
        return json($result, 200);

    }

}